package com.laint.hear.ui.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.laint.hear.R;
import com.laint.hear.app.AppStatusTracker;
import com.laint.hear.ui.activity.MainActivity;
import com.laint.hear.utils.ConstantsValue;

import butterknife.ButterKnife;

/**
 * Created by leiyingtao on 2016/10/12.
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected final static int MODE_NONE = 0;
    protected final static int MODE_BACK = 1;
    protected final static int MODE_HOME = 2;
    protected final static int MODE_DRAWER = 3;
    protected Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        switch (AppStatusTracker.getInstance().getmAppStatus()){
            case ConstantsValue.KILLED:
                protectApp();
                break;
            case ConstantsValue.ONLINE:
            case ConstantsValue.OUTLINE:
                setUpContentView();
                ButterKnife.bind(this);
                if(savedInstanceState==null){
                    setUpData();
                }else{
                    recoveryState(savedInstanceState);
                }
                break;
            case ConstantsValue.KICK_OUT:
                kickOut();
                break;
        }
    }

    protected void recoveryState(Bundle savedInstanceState) {
    }
    protected abstract void setUpContentView();
    protected abstract void setUpData();

    @Override
    public void setContentView(int layoutResID) {
        setContentView(layoutResID,-1,-1,MODE_BACK);
    }

    public void setContentView(int layoutResID, int titleResID){
        setContentView(layoutResID,titleResID,-1,MODE_BACK);
    }
    public void setContentView(int layoutResID,int titleResID,int mode){
        setContentView(layoutResID,titleResID,-1,mode);
    }
    public void setContentView(int layoutResID,int titleResID,int menuID,int mode){
        super.setContentView(layoutResID);
        setUpToolBar(titleResID,menuID,mode);
    }
    protected void setUpToolBar(int titleResId,int menuID,int mode){
        if(mode!=MODE_NONE){
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setTitle(titleResId);
            setSupportActionBar(toolbar);
            if(mode==MODE_BACK){
                toolbar.setNavigationIcon(R.drawable.back);
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onNavigationOnClick();
                    }
                });
            }
        }

    }

    protected void onNavigationOnClick(){
        finish();
    }

    /***
     * APP被强杀，重走应用流程
     */
    protected void protectApp() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(ConstantsValue.APP_STATUS,ConstantsValue.KILLED);
        startActivity(intent);
    }

    protected void kickOut() {
        //TODO go home

    }

}
